package com.jumi.microservice.stock.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.stock.domain.*;
import com.jumi.microservice.stock.service.StockConversionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @ClassName StockConversionController
 * @Author Abel Yang
 * @Date 2020/9/16/016 15:07
 **/
@Api(tags = {"【仓库管理】调仓(仓库内库存转换)相关接口"})
@RequestMapping("/supply/conversion")
@RestController
public class StockConversionController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(StockTransferController.class);

    @Resource
    private StockConversionService stockConversionService;

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockConversionApplyDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockConversionApplyDTO")})
    @ApiOperation("添加申请")
    @PostMapping("/apply")
    @WebLog(description = "转换添加申请")
    public ResponseResult<Boolean> addConversion(@RequestBody StockConversionApplyDTO stockConversionApplyDTO, HttpServletRequest request) {
        try {
            if (stockConversionApplyDTO.getConversionApplyDetailList().size() == 0) {
                throw new Exception("物料变动列表不能为空");
            }
            stockConversionService.addConversion(stockConversionApplyDTO, request);
        } catch (Exception e) {
            logger.error("stock conversion add apply StockConversionApplyDTO:{}", stockConversionApplyDTO);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("添加成功", true);
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockConversionSearchDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockConversionSearchDTO")})
    @ApiOperation("转换查询分页列表")
    @GetMapping("/getListByPage")
    public TableDataInfo<List<StockConversionVO>> getConversionList(StockConversionSearchDTO searchDTO) {
        startPage();
        List<StockConversionVO> conversionList = stockConversionService.getConversionList(searchDTO);
        return getDataTable(conversionList);
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "conversionId", value = "申请记录id", dataType = "java.lang.Long")})
    @ApiOperation("查看详情")
    @GetMapping("/detail")
    public ResponseResult<StockConversionDetailVO> getConversionDetail(Long conversionId) {
        try {
            StockConversionDetailVO stockConversionDetailVO = stockConversionService.getConversionDetail(conversionId);
            return ResponseResult.success(stockConversionDetailVO);
        } catch (Exception e) {
            return ResponseResult.error(e.getMessage());
        }
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockConversionApplyDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockConversionApplyDTO")})
    @ApiOperation("编辑申请")
    @PostMapping("/edit")
    @WebLog(description = "转换编辑申请")
    public ResponseResult editConversion(@Validated @RequestBody StockConversionApplyDTO apply, HttpServletRequest request) {
        try {
            if (apply.getConversionApplyDetailList().size() == 0) {
                throw new Exception("物料变动列表不能为空");
            }
            stockConversionService.editConversion(apply, request);
        } catch (Exception e) {
            logger.error("stock conversion edit apply StockConversionApplyDTO:{}", apply);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("编辑成功", true);
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockConversionAuditDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockConversionAuditDTO")})
    @ApiOperation("审核申请")
    @PostMapping("/audit")
    @WebLog(description = "转货审核申请")
    public ResponseResult<Boolean> auditConversion(@Validated @RequestBody StockConversionAuditDTO auditDTO, HttpServletRequest request) {
        try {
            if (auditDTO.getConversionIds().size() == 0) {
                throw new Exception("参数不能为空");
            }
            stockConversionService.auditConversion(auditDTO, request);
        } catch (Exception e) {
            logger.error("stock conversion audit StockConversionAuditDTO:{}", auditDTO);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("操作成功", true);
    }
}
